#include "newDrawingButton.h"
#include "drawingWidget.h"
#include "particleSystem.h"
#include "brush.h"
#include "input.h"
#include "ragDoll.h"
#include "particleSystem.h"
#include "skeletalDrawing.h"

NewDrawingButton::NewDrawingButton(){

textureID="icon_new";
}

void NewDrawingButton::mouseOver(){
}

void NewDrawingButton::clickedLeft(){
input->focusButton=this;
//link to parent
widge=(DrawingWidget*)parent;
}

void NewDrawingButton::focusClick(){
Button::focusClick();

    if (input->worldTarget->name!="ragDoll")
      setupBasicDrawing();
    else
      setupCharacterDrawing();
    //setup brush and connect
}

void NewDrawingButton::setupBasicDrawing(){

        //setup drawing
        ParticleSystem *drawing = new SkeletalDrawing;
        //drawing->bPickable=false;
        drawing->name="skeletalDrawing";
        drawing->location=input->mouse3D;
        drawing->controller=input->controller;
        //drawing->texture=renderer->textureList[1];
        drawing->setup();
        renderer->actorList.push_back(drawing);
        //widge->drawing=drawing;

            if (DrawingWidget::brush)
              {
              DrawingWidget::brush->remove();
              DrawingWidget::brush=NULL;
              }
        DrawingWidget::brush = new Brush;
        DrawingWidget::brush->drawing=drawing;
        DrawingWidget::brush->bPickable=false;
        DrawingWidget::brush->name="brush";
        DrawingWidget::brush->controller=input->controller;

        DrawingWidget::brush->menuType.empty();
        renderer->actorList.push_back(DrawingWidget::brush);
}

void NewDrawingButton::setupCharacterDrawing(){


    map<string,PhysicsActor*>::iterator it;
    RagDoll * myRagDoll=(RagDoll*)input->worldTarget;


    widge->createGrid(myRagDoll->location);

    DrawingWidget::grid->location+=DrawingWidget::grid->scale*Vector3f(0.5,0,-0.01);

    myRagDoll->menuType.empty();
      //create a ParticleSystem for every Part
    for ( it=myRagDoll->parts.begin() ; it != myRagDoll->parts.end(); it++ )
      {
        PhysicsActor * pActor = (*it).second;
        pActor->drawing= new ParticleSystem;
        pActor->drawing->setup();
        //pActor->drawing->addRandomParticles(5,2);
      }

    if (DrawingWidget::brush)
        {
        DrawingWidget::brush->remove();
        DrawingWidget::brush=NULL;
        }
    DrawingWidget::brush = new Brush;
    DrawingWidget::brush->drawCharacter=myRagDoll;
    DrawingWidget::brush->bPickable=false;
    DrawingWidget::brush->name="brush";
    DrawingWidget::brush->controller=input->controller;

    DrawingWidget::brush->menuType.empty();
    renderer->actorList.push_back(DrawingWidget::brush);
}


void NewDrawingButton::create(){

renderer->addButton(this);
std::cout << "creating a NewDrawingButton!";
}
